//
//  LlViewController.m
//  豆瓣电影app
//
//  Created by 洛洛大人 on 16/6/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LlViewController.h"
#import "MovieModel.h"
#import "llCollectionViewCell.h"
#import "UIView+WebCacheOperation.h"
#import "MovielistViewController.h"

@interface LlViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)NSMutableArray *leoOfArray;

@end

@implementation LlViewController
-(void)dealloc {
    [_leoOfArray release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"电影";
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.41 green:0.61 blue:0.68 alpha:1];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    
    [self handleData];
    
    [self creatCollectionView];
}

-(void)rightAction:(UIBarButtonItem *)item{
    
    MovielistViewController *llListvc = [[MovielistViewController alloc]init];
    [self.navigationController pushViewController:llListvc animated:YES];
    [llListvc release];
}



-(void)handleData{
    
    self.leoOfArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *llArray = [dic objectForKey:@"result"];

    for (NSDictionary *lldic in llArray) {
        MovieModel *llModel = [[MovieModel alloc]init];
        
        [llModel setValuesForKeysWithDictionary:lldic];
        
        [self.leoOfArray addObject:llModel];
        
    }
   
}


-(void)creatCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake(100, 150);
    
    flowLayout.minimumLineSpacing = 50;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(30, 20, 20, 20);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *llCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    llCollectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:llCollectionView];
    
    [flowLayout release];
    
    [llCollectionView release];
    
    llCollectionView.delegate = self;
    llCollectionView.dataSource = self;
    
    [llCollectionView registerClass:[llCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.leoOfArray.count;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    llCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.contentView.backgroundColor = [UIColor greenColor];
    
    MovieModel *leoModel = [self.leoOfArray objectAtIndex:indexPath.row];
    
    cell.cellModel = leoModel;
    
    return cell;
}







@end
